// pages/Census/customer-list.js
const Controllers = require('../../controllers/ControllerCensus').controller;
const agentIndex = require('../../controllers/Controller').controller.agentIndex;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        value: 0,
        text: '下级服务商商租金收益',
        tabText: '代理商租金收益',
        key: 'rent',
        status: 8
      },
      {
        value: 0,
        text: '下级服务商云商品收益',
        tabText: '服务商云商品收益',
        key: 'cloud',
        status: 7,
        className: 'fontColor',
      }
    ],
    active: 0,
    shopId: '',
    page: {
      pageNumber: 1,
      pageSize: 20,
      genre: 0
    },
    agentIndexData: {}
  },
  agentIndex() {
    var that = this;
    const shopId = that.data.shopId;
    agentIndex({shopId}).then(res => {
      console.log('agentIndex--res==', res);
      const { code, data, desc } = res;
      app.globalData.agentIndexData = data;
      that.setData({
        agentIndexData: data
      })
    })
  },
  sonAgentIncome() {
    var that = this;
    const shopId = that.data.shopId;
    Controllers.sonAgentIncome({shopId}).then(res => {
      console.log('sonAgentIncome--res==', res);
      const { code, data, desc } = res;
      that.setData({
        sonAgentIncome: data
      })
    })
  },
  sonAgentIncomeDetails() {
    var that = this;
    const shopId = that.data.shopId;
    const tabs = that.data.tabs;
    const active = that.data.active;
    const page = that.data.page;
    wx.showLoading();
    var params = { ...page, type: tabs[active].status }
    Controllers.sonAgentIncomeDetails(params).then(res => {
      console.log('sonAgentIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { list, pageNumber } = data;
      if (pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      that.setData({
        list: that.data.list 
      })
      wx.hideLoading()
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      active: parseInt(options.active)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.sonAgentIncome();
    that.sonAgentIncomeDetails();
  },
  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index !== that.data.active) {
      that.data.page.pageNumber = 1;
      that.sonAgentIncomeDetails();
    }
    that.setData({
      active: index,
      page: that.data.page
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})